package gui.frame;

import java.awt.Button;
import java.awt.CheckboxMenuItem;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextField;

public class FrameMenu extends Frame {
	String[] flavors = { "Chocolate", "Strawberry", "Mocha", "Mint" };
	TextField tf=new TextField("No flavor",30);
	MenuBar mb=new MenuBar();
	Menu fileMenu=new Menu("File");
	Menu menu=new Menu("Flavors");
	Menu safe=new Menu("Safety");
	
	CheckboxMenuItem[] safety={
		new CheckboxMenuItem("Guard"),
		new CheckboxMenuItem("Hide")
	};
	
	MenuItem[] files={
			new MenuItem("Open"),
			new MenuItem("Exit")
	};
	
	MenuBar mb2=new MenuBar();
	Menu fooBar=new Menu("fooBar");
	
	MenuItem[] other={
			new MenuItem("Foo"),
			new MenuItem("Bar"),
			new MenuItem("Baz")
	};
	
	Button btn=new Button("Swap Menus");
	public FrameMenu(){
		for(int i=0;i<flavors.length;i++){
			this.menu.add(new MenuItem(flavors[i]));
			
			if((i+1)%3==0)
				this.menu.addSeparator();
		}
		
		for(int i=0;i<safety.length;i++){
			this.safe.add(safety[i]);
		}
		
		this.fileMenu.add(safe);
		
		for(int i=0;i<safety.length;i++){
			this.fileMenu.add(this.files[i]);
		}
		this.mb.add(this.fileMenu);
		this.mb.add(this.menu);
		this.setMenuBar(this.mb);
		
		this.tf.setEditable(false);
		this.add("Center",this.tf);
		
		this.add("North",this.btn);
		
		for(int i=0;i<other.length;i++){
			this.fooBar.add(other[i]);
		}
		
		this.mb2.add(this.fooBar);
		
	}

	public boolean handleEvent(Event evt){
		if(evt.id==Event.WINDOW_DESTROY)
			System.exit(0);
		else
			return super.handleEvent(evt);
		return true;		
	}
	
	public boolean action(Event evt,Object obj){
		if(evt.target.equals(btn)){
			MenuBar mb3=this.getMenuBar();
			if(mb3==mb)
				this.setMenuBar(mb2);
			else if(mb3==mb2)
				this.setMenuBar(mb);
		}else if(evt.target instanceof  MenuItem){
			if(obj.equals("Open")){
				String s= tf.getText();
				boolean chosen=false;
				for(int i=0;i<flavors.length;i++){
					if(s.equals(flavors[i]))
						chosen=true;
					if(!chosen){
						tf.setText("Chosen a flavor first!");
					}
					else 
						tf.setText("Open "+s +".Mmm,mm!");
				}
			}
			else if( evt.target.equals(files[1]))
				System.exit(0);
			else if(evt.target.equals(safety[0]))
				tf.setText("Guard the Ice Cream! Guarding is "+safety[0].getState());
			else
				tf.setText(obj.toString());
		}
		else
			return super.action(evt, obj);
		return true;
	}
	
	public static void main(String[]args){
		FrameMenu fm=new FrameMenu();
		fm.resize(300,400);
		fm.show();
	
	}
}
